#region Using Statements
using System;
using System.Threading;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion


namespace GameFramework.ScreenSystem
{
    /// <summary>
    /// This screen implements the actual game logic. It is just a
    /// placeholder to get the idea across: you'll probably want to
    /// put some more interesting gameplay in here!
    /// </summary>
    class GameplayScreen : GameScreen
    {
        #region Fields

        ContentManager content;
        SpriteFont gameFont;

        Vector2 playerPosition = new Vector2(100, 100);
        Vector2 enemyPosition = new Vector2(100, 100);

        Random random = new Random();

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public GameplayScreen(string musicToPlay)
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            if(!String.IsNullOrEmpty(musicToPlay))
                GameManager.AudioManager.Play(musicToPlay);
        }


        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                if (content == null)
                    content = new ContentManager(ScreenManager.Game.Services);

                gameFont = content.Load<SpriteFont>("Content/Fonts/gamefont");

                // A real game would probably have more content than this sample, so
                // it would take longer to load. We simulate that by delaying for a
                // while, giving you a chance to admire the beautiful loading screen.
                Thread.Sleep(1000);
            }
        }


        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
                content.Unload();
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                // Apply some random jitter to make the enemy move around.
                const float randomization = 10;

                enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
                enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;

                // Apply a stabilizing force to stop the enemy moving off the screen.
                Vector2 targetPosition = new Vector2(200, 200);

                enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);

                // TODO: this game isn't very fun! You could probably improve
                // it by inserting something more interesting in this space :-)
            }
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput(InputSystem.InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            if (input.PauseGame)
            {
                // Stop all currently playing sounds so there is silence between transitions.
                GameManager.AudioManager.StopAllPlaying();
                
                // If they pressed pause, bring up the pause menu screen. With the cue name
                // Play the "PauseMenuMusic" cue while the pause menu is being displayed.
                ScreenManager.AddScreen(new PauseMenuScreen("PauseMessageBoxMusic",MenuTransition.Transition4));
            }
            else
            {
                // Otherwise move the player position.
                const float movementSpeed = 2;

                if (input.CurrentKeyboardState.IsKeyDown(Keys.Left))
                    playerPosition.X -= movementSpeed;

                if (input.CurrentKeyboardState.IsKeyDown(Keys.Right))
                    playerPosition.X += movementSpeed;

                if (input.CurrentKeyboardState.IsKeyDown(Keys.Up))
                    playerPosition.Y -= movementSpeed;

                if (input.CurrentKeyboardState.IsKeyDown(Keys.Down))
                    playerPosition.Y += movementSpeed;

                Vector2 thumbstick = input.CurrentGamePadState.ThumbSticks.Left;

                playerPosition.X += thumbstick.X * movementSpeed;
                playerPosition.Y -= thumbstick.Y * movementSpeed;
            }
        }


        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            // This game has a blue background. Why? Because!
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.CornflowerBlue, 0, 0);

            // Our player and enemy are both actually just text strings.
            ScreenManager.SpriteBatch.Begin();

            ScreenManager.SpriteBatch.DrawString(gameFont, "// TODO",
                                                 playerPosition, Color.Green);

            ScreenManager.SpriteBatch.DrawString(gameFont, "Insert Gameplay Here",
                                                 enemyPosition, Color.DarkRed);

            ScreenManager.SpriteBatch.End();

            // If the game is transitioning on or off, fade it out to black.
            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
        }


        #endregion
    }
}
